package vn.vnu.uet.university;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import vn.vnu.uet.university.bean.DatabaseHandler;
import vn.vnu.uet.university.bean.TinTuc;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class News extends DefaultActivity {
	ListView listContent;
	List<TinTuc> tinTucList = new ArrayList<TinTuc>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState, R.layout.news);
		
		DatabaseHandler db = new DatabaseHandler(News.this);

		this.setTitle("Tin tức");
		listContent = (ListView) findViewById(R.id.listContent);

		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
	
		tinTucList = db.getAllTinTuc();
		
		for(int i = 0; i < tinTucList.size(); i++) {
			map = new HashMap<String, String>();
			map.put("item_icon", String.valueOf(R.drawable.card_default));
			map.put("item_text", tinTucList.get(i).getTitle());
			listItem.add(map);
		}

		SimpleAdapter mSchedule = new SimpleAdapter(News.this, listItem,
				R.layout.row_item, new String[] { "item_icon", "item_text" },
				new int[] { R.id.item_icon, R.id.item_text });
		listContent.setAdapter(mSchedule);
		
		listContent.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Intent intent = new Intent(News.this, ViewContent.class);
				intent.putExtra("position", tinTucList.get(position).getId());
				intent.putExtra("class", 1);
				startActivity(intent);
			}
		});
	}
}